package com.hefu.virtuallife.ui.foods;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.dz.utlis.ClassTools;
import com.dz.utlis.JavaUtils;
import com.hefu.virtuallife.R;
import com.hefu.virtuallife.baseui.BaseActivity;
import com.hefu.virtuallife.baseui.BaseFragment;
import com.hefu.virtuallife.ui.foods.adapter.FoodPlanViewPageAdapter;
import com.hefu.virtuallife.ui.foods.adapter.MyMenuePageAdapter;
import com.hefu.virtuallife.ui.foods.fragment.MyCustMenueFragment;
import com.hefu.virtuallife.ui.foods.fragment.MyFoodPlanFragment;
import com.hefu.virtuallife.ui.foods.fragment.MyPreMenueFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * creat_user: zhengzaihong
 * Email:1096877329@qq.com
 * describe 我的菜单
 **/
public class MyMenueActivity extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageButton ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_addmenue)
    TextView tvAddmenue;
    @BindView(R.id.mTableLayout)
    TabLayout mTableLayout;
    @BindView(R.id.mViewPager)
    ViewPager mViewPager;

    private int position = 0;

    private List<BaseFragment> listfragment = new ArrayList<>();

    private MyMenuePageAdapter myMenuePageAdapter;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_my_menue_foods;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText("我的菜单");
        ivBack.setOnClickListener(v -> finish());
    }


    @Override
    protected void initData() {
        super.initData();

        BaseFragment premenue = new MyPreMenueFragment();
        premenue.setTitlefragment("预订菜单");
        listfragment.add(premenue);

        BaseFragment custmenue = new MyCustMenueFragment();
        custmenue.setTitlefragment("自定义菜单");
        listfragment.add(custmenue);
        myMenuePageAdapter = new MyMenuePageAdapter(getSupportFragmentManager(), listfragment);

        mViewPager.setAdapter(myMenuePageAdapter);
        mViewPager.setOffscreenPageLimit(listfragment.size());
        mTableLayout.setTabMode(TabLayout.MODE_FIXED);
        mTableLayout.setupWithViewPager(mViewPager);

        mTableLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int p = tab.getPosition();
                position = p;
                JavaUtils.outPrint("mTableLayout:" + p);
                if (p == 0) {
                    tvAddmenue.setText("添加菜单");
                } else {
                    tvAddmenue.setText("添加自定义菜单");
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        tvAddmenue.setOnClickListener(view -> {
            if (position != 0) {
                ClassTools.toAct(mContext, MyCustMenueActivity.class);
            } else {
                this.finish();
            }
        });
    }

}
